package classobject;

import java.util.*;

class HashCodeMethod {
	public static void main(String[] args) {

		HashMap<Person, String> map = new HashMap<Person, String>();
		Person p1 = new Person(1, "Key1");
		Person p2 = new Person(2, "Key2");
		Person p3 = new Person(1, "Key3");
		
		System.out.println("Adding Entries ....");
		map.put(p1, "Value1");
		map.put(p2, "Value2");
		map.put(p3, "Value3");

		System.out.println("\nComplete Map entries \n" + map);

		System.out.println("\nAccessing non-collided key");
		System.out.println("Value = " + map.get(p2));
		System.out.println("\nAccessing collided key");
		System.out.println("Value = " + map.get(p1));
	}
}

class Person {
	private int id;
	private String name;

	public Person(int id, String name) {
		this.id = id;
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int hashCode() {
		System.out.println("called hashCode for =" + id + "." + name);
		return id;
	}

	public boolean equals(Object obj) {
		System.out.println("called equals on =" + id + "." + name
				+ "  to compare with  = " + ((Person) obj).getId() + "."
				+ ((Person) obj).getName());
		boolean result = false;
		if (obj instanceof Person) {
			if (((Person) obj).getId() == id
					&& ((Person) obj).getName().equals(name))
				result = true;
		}
		return result;
	}

	public String toString() {
		return id + " - " + name;
	}
}